package com.flashphoner.api.management
{
		
	import com.flashphoner.Logger;
	import com.flashphoner.api.data.PhoneConfig;
	
	import flash.events.*;
	import flash.media.*;
	import flash.net.NetConnection;
	import flash.net.NetStream;
	
	import mx.core.UIComponent;
	
	/**
	 * Phone speaker implementation
	 * **/	
	public class PhoneSpeaker extends UIComponent
	{	
		private var videoContainer:UIComponent;		
		private var video:Video;
		private var incomingStream:NetStream;
		private var incomingVideoStream:NetStream;
		private var netConnection:NetConnection;
		public  var playing:Boolean=false;
		public var playingVideo:Boolean=false;
		public  var currentVolume:int = 100;
		
		
		public function PhoneSpeaker(netConnection:NetConnection):void
		{
			this.netConnection = netConnection;
			video = new Video();
			addChild(video);			
			video.width = 176;
			video.height = 144;
			video.clear();			
		}
		
		private function nsOnStatus(infoObject:NetStatusEvent):void
		{
			Logger.info("PhoneSpeaker.nsOnStatus()");
			
			if (incomingStream==null){
				return;
			}			
					
			if (infoObject.info.code == "NetStream.Play.Start"){
				playing = true;
			}		
					
			else if (infoObject.info.code == "NetStream.Play.StreamNotFound" || infoObject.info.code == "NetStream.Play.Failed"||infoObject.info.code == "NetStream.Play.Stop"){
				Logger.info("incomingStream.onStatus() "+infoObject.info.description);
				playing = false;
			}
				
		}
		
		private function nsVideoOnStatus(infoObject:NetStatusEvent):void
		{
			Logger.info("PhoneSpeaker.nsVideoOnStatus()");
			
			if (incomingVideoStream==null){
				return;
			}			
					
			if (infoObject.info.code == "NetStream.Play.Start"){
				playingVideo = true;
			}		
					
			else if (infoObject.info.code == "NetStream.Play.StreamNotFound" || infoObject.info.code == "NetStream.Play.Failed"||infoObject.info.code == "NetStream.Play.Stop"){
				Logger.info("incomingVideoStream.nsVideoOnStatus() "+infoObject.info.description);
				playingVideo = false;
			}
				
		}
		
		public function stop():void{
			Logger.info("PhoneSpeaker.stop()");
			if (incomingStream!=null){				
				incomingStream.removeEventListener(NetStatusEvent.NET_STATUS, nsOnStatus);
				try{
					incomingStream.play(false);
				}catch(error:Error){
					Logger.error("Can not stop stream: "+error);
				}
				incomingStream = null;
				playing = false;
			}
			if ((incomingVideoStream!=null)&&(PhoneConfig.VIDEO_ENABLED)){
				incomingVideoStream.removeEventListener(NetStatusEvent.NET_STATUS, nsVideoOnStatus);
				try{
					incomingVideoStream.play(false);
				}catch(error:Error){
					Logger.error("Can not stop video stream: "+error);
				}
				incomingVideoStream = null;	
				playingVideo = false;
			}		
			
			video.clear();
			video.attachNetStream(null);			
		}
		
		public function play(streamName:String):void{
			Logger.info("PhoneSpeaker play streamName="+streamName+" playing="+playing);
			if (!playing){
				if (incomingStream==null){
					incomingStream = startNewIncomingStream(streamName,nsOnStatus);
				}				
			}	
		}
		
		public function playVideo(streamName:String):void{
			Logger.info("PhoneSpeaker playVideo streamName="+streamName+" playingVideo="+playingVideo);
			if (!playingVideo){
				if (incomingVideoStream==null){
					incomingVideoStream = startNewIncomingStream(streamName,nsVideoOnStatus);
				}				
			}	
		}
		
		private function startNewIncomingStream(streamName:String, listener:Function):NetStream{
			
			Logger.info("PhoneSpeaker startNewIncomingStream streamName="+streamName);
			
			var stream:NetStream = new NetStream(netConnection);
			
			stream.addEventListener(NetStatusEvent.NET_STATUS, listener);
			
			var streamClientObj:Object = new Object();
			stream.client = streamClientObj;
			stream.bufferTime = 0;
			
			Logger.info("PhoneSpeaker startNewIncomingStream bufferTime "+stream.bufferTime);
				
			var soundTransform:SoundTransform = new  SoundTransform;	
			soundTransform.volume=currentVolume/100;
						
			stream.soundTransform = soundTransform;					
			stream.play(streamName);
						
			video.attachNetStream(stream);
						
			return stream;			
		}
				
		public function getVolume():int{
			return currentVolume;
		}		
		
		public function setVolume(volume:int):void{
			currentVolume = volume;
			if (incomingStream != null){
				var soundTransform:SoundTransform = new  SoundTransform;	
				soundTransform.volume = currentVolume/100;
				incomingStream.soundTransform = soundTransform;
			}
						
		}
						
	}
}